
var processDefinitionId = "";
var processDefinitionName = "";
var processDefinitionVariables = "";
var _process_def_provided_listeners = "";
var is_open_properties_panel = false;
var task;
var line;
var canvas;
var container;
$(window).load(function () {
    _designer = $('#designer');
    _properties_panel_obj = _designer.layout('panel',
        'east');
    _properties_panel_obj.panel({
        onOpen: function () {
            is_open_properties_panel = true;
        },
        onClose: function () {
            is_open_properties_panel = false;
        }
    });
    _process_panel_obj = _designer
        .layout('panel', 'center');
    _designer.layout('collapse', 'east');
    var height = _process_panel_obj.panel('options').height;
    var width = _process_panel_obj.panel('options').width;
    canvas = new ESTDesigner.Canvas("paintarea",
        width + 300, height);
    var start = new ESTDesigner.event.Start();
    var end = new ESTDesigner.event.End();
    canvas.add(start, 200, 50);
    canvas.add(end, 200, 400);
    //注册canvas右键菜单处理回调函数
    canvas.callback = function (id) {
        if (!is_open_properties_panel)
            _designer.layout('expand', 'east');
        _properties_panel_obj.panel('refresh',
            'processProperties.html');
    };
    //注册connection右键菜单处理回调函数
    canvas.connectionCallback = function (obj) {
        if (!is_open_properties_panel)
            _designer.layout('expand', 'east');

        line = obj;
        _properties_panel_obj.panel('refresh',
            'flowProperties.html');
    };
    //task右键菜单处理函数
    canvas.taskCallback = function (t, key) {
        if (!is_open_properties_panel)
            _designer.layout('expand', 'east');
        task = t;
        if (task.type == "ESTDesigner.task.UserTask") {
            _properties_panel_obj.panel('refresh',
                'userTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.ManualTask") {
            _properties_panel_obj.panel('refresh',
                'manualTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.ServiceTask") {
            _properties_panel_obj.panel('refresh',
                'serviceTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.ScriptTask") {
            _properties_panel_obj.panel('refresh',
                'scriptTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.ReceiveTask") {
            _properties_panel_obj.panel('refresh',
                'receiveTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.MailTask") {
            _properties_panel_obj.panel('refresh',
                'mailTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.BusinessRuleTask") {
            _properties_panel_obj.panel('refresh',
                'businessRuleTaskProperties.html');
        } else if (task.type == "ESTDesigner.task.CallActivityTask") {
            _properties_panel_obj.panel('refresh',
                'callActivityProperties.html');
        }
    };
    //container右键菜单处理函数
    canvas.containerCallback = function (obj) {
        if (!is_open_properties_panel) {
            _designer.layout('expand', 'east');
        }
        container = obj;
        _properties_panel_obj.panel('refresh',
            'containerProperties.html');
    };
    $('.easyui-linkbutton')
        .draggable(
            {
                proxy: function (source) {
                    var n = $('<div class="draggable-model-proxy"></div>');
                    n.html($(source).html())
                        .appendTo('body');
                    return n;
                },
                deltaX: 0,
                deltaY: 0,
                revert: true,
                cursor: 'auto',
                onStartDrag: function () {
                    $(this).draggable('options').cursor = 'not-allowed';
                },
                onStopDrag: function () {
                    $(this).draggable('options').cursor = 'auto';
                }
            });
    $('#paintarea')
        .droppable(
            {
                accept: '.easyui-linkbutton',
                onDragEnter: function (e, source) {
                    $(source).draggable('options').cursor = 'auto';
                },
                onDragLeave: function (e, source) {
                    $(source).draggable('options').cursor = 'not-allowed';
                },
                onDrop: function (e, source) {
                    var wfModel = $(source).attr(
                        'wfModel');
                    var shape = $(source).attr(
                        'iconImg');
                    var modelType = $(source).attr(
                        'modelType');
                    if (wfModel) {
                        var x = $(source)
                            .draggable('proxy')
                            .offset().left;
                        var y = $(source)
                            .draggable('proxy')
                            .offset().top;
                        var xOffset = canvas
                            .getAbsoluteX();
                        var yOffset = canvas
                            .getAbsoluteY();
                        var scrollLeft = canvas
                            .getScrollLeft();
                        var scrollTop = canvas
                            .getScrollTop();
                        addModel(wfModel, x
                            - xOffset
                            + scrollLeft, y
                            - yOffset
                            + scrollTop, shape,
                            modelType);
                    }
                }
            });
    function addModel(name, x, y, icon, type) {
        var model = null;
        model = eval("new " + name);
        canvas.add(model, x, y);
    }

    //产生xml
    $('#process-definition-tab').tabs({
        fit: true,
        onSelect: function (title) {
            if (title == 'XML') {
                $('#descriptorarea').text(canvas.toXML());
                $('#descriptorarea').each(function (i, block) {
                    hljs.highlightBlock(block);
                });
            }
        }
    });
    $('#saveWfBt').click(function () {
        var xml = canvas.toXML();
        var data = {
            xml: xml,
            name: canvas.process.name,
            processVariables: canvas.process.getVariablesJSONObject()
        };
        Http.post(Config.Url.Save_Wf, data, EventHandler.OnSaveWf.Success, EventHandler.OnSaveWf.Error);
    });
    canvas.getCommandStack().addEventListener(function (e) {
        if (e.isPostChangeEvent()) {
            updatePreview(canvas);
        }
    });
    updatePreview(canvas);
});
function redo() {
    canvas.getCommandStack().redo();
}
function undo() {
    canvas.getCommandStack().undo();
}

function exportProcessDef(obj) {
    $("#exportProcessDialog").dialog('open');
    updatePreview(canvas);
}
function openImportDialog(obj) {
    $("#importProcessDialog").dialog('open');
}
function importProcDef() {
    canvas.clear();
    var xml = $('#importXml').val();
    var parser = new ESTDesigner.tool.Parser(canvas, xml);
    parser.parse();
    $('#importXml').val('');
    $("#importProcessDialog").dialog('close');
}
function closeImportDialog() {
    $('#importXml').val('');
    $("#importProcessDialog").dialog('close');
}
function closeExportDialog() {
    $("#exportProcessDialog").dialog('close');
}
function updatePreview(canvas) {
    var writer = new draw2d.io.png.Writer();
    writer.marshal(canvas, function (png) {
        $("#preview").attr("src", png);
    });
};